package mytool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JOptionPane;

/**
 * 先用能够连网的ip试，看是否能连接
@echo off
netsh interface ip set address "本地连接 2" static 172.16.6.1 255.255.0.0 172.16.6.1 1
netsh interface ip set dns "本地连接 2" static 202.96.133.134
netsh interface ip add dns "本地连接 2" static 202.96.128.167 2

使用eclipse连接来获取是否连接
 *
@echo off
echo 自动获取ip地址
netsh interface ip set address name="本地连接 2" source=dhcp
echo 自动获取DNS服务器。。。注意：name="本地连接 2"中间不能有空格
netsh interface ip set dns name="本地连接 2" source=dhcp
echo 自动获取ip成功，按任一键后，就可以使用外网了
pause
 */
public class CrackIp {
	public static void main(String[] args) throws Exception {
		//Exception in thread "main" java.net.SocketException: Network is unreachable: connect
		//URL url= new URL("http://www.google.com/");//Exception in thread "main" java.net.ConnectException: Connection timed out: connect
//		URL url = new URL("http://mp3.com/mylove.mp3");
//		InputStream is = url.openConnection().getInputStream();
		for (int i = 0; i < 256; i++) {
			String file="crackIp"+i+".bat";
			File scriptFile= new File(file);
			FileOutputStream out = new FileOutputStream(scriptFile);
			int count=0;
			byte[] script1="@echo off".getBytes("gbk");
			byte[] script2=("netsh interface ip set address \"本地连接 2\" static 172.16.6."+i+" 255.255.0.0 172.16.6.1 1").getBytes("gbk");
			out.write(script1);
			out.write(13);//(int)'\r'(int)'\n'
			out.write(10);
			out.write(script2);
			out.flush();
			out.close();
			Thread.sleep(10000);
			Runtime.getRuntime().exec("crackIp"+i+".bat");
			while(true){
				try {
					URL url = new URL("http://www.google.com/");
					URLConnection con = url.openConnection();
					con.connect();
					JOptionPane.showMessageDialog(null, "连接成功");
				} catch (ConnectException e) {
					count++;
					if(count==2)
						break;
				}catch(SocketException e){
					break;
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		
	}
}
